﻿Imports DevExpress.XtraEditors

Public Class frmGroup
    Dim data As DataTable
    Public Sub loadGroup()
        Try
            data = mysql_query("SELECT a.id_grup_kontak,a.grup_kontak,COUNT(tb_contact_group_member.`id_grup_kontak`) AS 'total' " & _
                                "FROM tb_contact_group AS a " & _
                                "LEFT JOIN tb_contact_group_member ON a.id_grup_kontak = tb_contact_group_member.id_grup_kontak " & _
                                "GROUP BY a.`id_grup_kontak` " & _
                                "ORDER BY grup_kontak")
            GridControlList.DataSource = data
            If data.Rows.Count > 0 Then
                SimpleButtonEdit.Enabled = True
                SimpleButtonDelete.Enabled = True
            Else
                SimpleButtonEdit.Enabled = False
                SimpleButtonDelete.Enabled = False
            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub frmGroup_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loadGroup()
    End Sub

    Private Sub SimpleButtonAdd_Click(sender As Object, e As EventArgs) Handles SimpleButtonAdd.Click
        frmGroup_Form.Dispose()
        frmGroup_Form.ShowDialog()
    End Sub

    Private Sub SimpleButtonRefresh_Click(sender As Object, e As EventArgs) Handles SimpleButtonRefresh.Click
        loadGroup()
    End Sub

    Private Sub SimpleButtonDelete_Click(sender As Object, e As EventArgs) Handles SimpleButtonDelete.Click
        If XtraMessageBox.Show("Are you sure want to delete this group?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
            Cursor = Cursors.WaitCursor
            Dim id As String = GridViewList.GetFocusedRowCellDisplayText("id_grup_kontak").ToString
            mysql_non_query(String.Format("DELETE FROM tb_contact_group WHERE id_grup_kontak = '{0}'", id))
            loadGroup()
            Cursor = Cursors.Default
        End If
    End Sub

    Private Sub SimpleButtonEdit_Click(sender As Object, e As EventArgs) Handles SimpleButtonEdit.Click
        Try
            frmGroup_Form.Dispose()
            frmGroup_Form.is_edit = True
            frmGroup_Form.id = CType(GridViewList.GetFocusedRowCellDisplayText("id_grup_kontak").ToString, Long)
            frmGroup_Form.ShowDialog()
        Catch ex As Exception
            XtraMessageBox.Show("Please Select One Item For Editing!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

End Class